/************************************************************************************/
/*  Program name CAPFNDIMP_CONC_PROG_PARAM.sql                                      */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

--ALTER TRIGGER FND_RESPONSIBILITY_CT1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDIMP_CONC_PROG_PARAM';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_CONC_PROG_PARAM;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	--  RENAME
	UPDATE
		FND_DESCR_FLEX_COLUMN_USAGES
	SET
		Descriptive_Flexfield_Name	=	NVL(CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,7,4),Descriptive_Flexfield_Name),
										CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,11,4),Descriptive_Flexfield_Name))
	WHERE	(Application_ID,Descriptive_Flexfield_Name,Descriptive_Flex_Context_Code,Application_Column_Name) IN
		(
			SELECT
				Application_ID,Descriptive_Flexfield_Name,Descriptive_Flex_Context_Code,Application_Column_Name
			FROM
				CRST_FND_CONC_PROG_PARAM
		);
		
	UPDATE
		FND_DESCR_FLEX_COLUMN_USAGES
	SET
		Descriptive_Flexfield_Name	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,11,4),Descriptive_Flexfield_Name)
	WHERE	(Application_ID,Descriptive_Flexfield_Name,Descriptive_Flex_Context_Code,Application_Column_Name) IN
		(
			SELECT
				Application_ID,Descriptive_Flexfield_Name,Descriptive_Flex_Context_Code,Application_Column_Name
			FROM
				CRST_FND_CONC_PROG_PARAM
		);

	FOR C1 IN Cur_F LOOP
		BEGIN
			INSERT INTO FND_DESCR_FLEX_COLUMN_USAGES
			VALUES
			(
			C1.APPLICATION_ID,
			C1.DESCRIPTIVE_FLEXFIELD_NAME,
			C1.DESCRIPTIVE_FLEX_CONTEXT_CODE,
			C1.APPLICATION_COLUMN_NAME,
			C1.END_USER_COLUMN_NAME,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATE_BY,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.COLUMN_SEQ_NUM,
			C1.ENABLED_FLAG,
			C1.REQUIRED_FLAG,
			C1.SECURITY_ENABLED_FLAG,
			C1.DISPLAY_FLAG,
			C1.DISPLAY_SIZE,
			C1.MAXIMUM_DESCRIPTION_LEN,
			C1.CONCATENATION_DESCRIPTION_LEN,
			CAPBRIMP_COMMON.GetValueSetID(C1.FLEX_VALUE_SET_NAME),	--FLEX_VALUE_SET_ID,
			C1.RANGE_CODE,
			C1.DEFAULT_TYPE,
			REPLACE(C1.DEFAULT_VALUE,'^C10^',CHR(10)),
			C1.SRW_PARAM,
			C1.RUNTIME_PROPERTY_FUNCTION
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

--ALTER TRIGGER FND_RESPONSIBILITY_CT1 ENABLE;
